package whisper.service.monitor.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import whisper.common.exception.format.PatternException;
import whisper.service.monitor.domain.ResponseBody;
import whisper.service.monitor.domain.ResponseCode;
import whisper.service.monitor.service.RoadPointDataService;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Created by JT on 2017/9/28.
 */
@RestController
@RequestMapping("/data/roadpoint")
public class RoadPointDataController {

    @Autowired
    private RoadPointDataService roadPointDataService;

    @GetMapping("/size")
    public ResponseBody findSizeByIssueTime(@RequestParam(name = "issuetime") String issuetime) {
        String pattern = "\\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d";
        boolean isMatch = Pattern.matches(pattern, issuetime);
        ResponseBody body = new ResponseBody();
        if (isMatch) {
            String count = roadPointDataService.findCountByIssueTime(issuetime);
            body.setCode(ResponseCode.STATUS_SUCCESS);
            body.setMessage("Success!");
            Map map = new HashMap();
            map.put("size", count);
            body.setData(map);
        } else {
            throw new PatternException(ResponseCode.STATUS_ERROR, "传入参数:issuetime格式有误！");
        }
        return body;
    }
}
